package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Ap0stoL2
 * Date: 2023-07-25
 * Time: 9:12
 */
// 线程锁
class Counter{
    public int count =0;

    public int count2=0;

    synchronized public void increase(){
        count++;
    }

    public void increase2(){
        count2++;
    }

}
public class Demo12 {
    public static void main(String[] args) throws InterruptedException {

        Counter counter = new Counter();


        //lambda能捕获类
        Thread t1 = new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
                counter.increase2();
            }
        });

        Thread t2 = new Thread(()->{
            for(int i=0;i<50000;i++){
                counter.increase();
                counter.increase2();
            }
        });


        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(counter.count);
        System.out.println(counter.count2);

    }
}
